package cn.exrick.xboot.modules.transport.service.impl;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.modules.transport.entity.CompanyRoutes;
import cn.exrick.xboot.modules.transport.entity.CompanyWarehouses;
import cn.exrick.xboot.modules.transport.mapper.CompaniesMapper;
import cn.exrick.xboot.modules.transport.entity.Companies;
import cn.exrick.xboot.modules.transport.mapper.CompanyRoutesMapper;
import cn.exrick.xboot.modules.transport.mapper.CompanyWarehousesMapper;
import cn.exrick.xboot.modules.transport.param.QueryCompaniesParam;
import cn.exrick.xboot.modules.transport.service.CompaniesService;
import cn.exrick.xboot.modules.transport.vo.CompaniesVo;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物流公司基本信息接口实现
 * @author gj
 */
@Slf4j
@Service
@Transactional
public class CompaniesServiceImpl extends ServiceImpl<CompaniesMapper, Companies> implements CompaniesService {

    @Autowired
    private CompaniesMapper companiesMapper;

    @Autowired
    private CompanyWarehousesMapper companyWarehousesMapper;

    @Autowired
    private CompanyRoutesMapper companyRoutesMapper;

    @Override
    public IPage<CompaniesVo> getByConditionPage(Page<Companies> page, QueryCompaniesParam param) {
        QueryWrapper<Companies> qw = new QueryWrapper<>();
        // 物流公司名称模糊搜素
        qw.like(StrUtil.isNotBlank(param.getCompanyName()), "company_name", param.getCompanyName());
        //按创建时间倒序
        qw.orderByDesc("create_time");
        Page<Companies> companiesPage = companiesMapper.selectPage(page, qw);
        Page pageResult = BeanConverterUtil.convertBean(companiesPage, Page.class);
        if (CollectionUtil.isNotEmpty(companiesPage.getRecords())){
            pageResult.setRecords(companiesPage.getRecords().stream().map(company -> {
                CompaniesVo companiesVo = BeanConverterUtil.convertBean(company, CompaniesVo.class);
                companiesVo.setWarehousesAmount(companyWarehousesMapper.selectCount(new LambdaQueryWrapper<CompanyWarehouses>().eq(CompanyWarehouses::getCompanyId, company.getId())));
                companiesVo.setRoutesAmount(companyRoutesMapper.selectCount(new LambdaQueryWrapper<CompanyRoutes>().eq(CompanyRoutes::getCompanyId, company.getId())));
                return companiesVo;
            }).collect(Collectors.toList()));
        }
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCompanyById(Long id) {
        // 公司下存在仓点不能删除
        if (companyWarehousesMapper.selectCount(new LambdaQueryWrapper<CompanyWarehouses>().eq(CompanyWarehouses::getCompanyId, id)) > 0){
            throw new RuntimeException("该物流公司存在仓点，无法删除");
        }
        // 公司下存在线路不能删除
        if (companyRoutesMapper.selectCount(new LambdaQueryWrapper<CompanyRoutes>().eq(CompanyRoutes::getCompanyId, id)) > 0){
            throw new RuntimeException("该物流公司存在线路，无法删除");
        }
        this.removeById(id);
    }
}